package com.kfm.usermanagement.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kfm.usermanagement.mapper.UserMapper;
import com.kfm.usermanagement.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.List;

public class UserService {

    private UserMapper mapper;

    public UserService() {
        try {
            mapper = new SqlSessionFactoryBuilder()
                    .build(Resources.getResourceAsStream("mybatis-config01.xml"))
                    .openSession()
                    .getMapper(UserMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public PageInfo<User> findAll(Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        // 开始分页
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = mapper.selectAll();
        return new PageInfo<>(list);
    }
}
